<?php 
/*
 module:		活动列表
 create_time:	2022-03-25 19:52:07
 author:		
 contact:		
*/

namespace app\backend\controller;
use app\api\model\Notice as NoticeModel;
use app\backend\service\NoticeService;
use think\facade\Db;

class Notice extends Admin {


	/*首页数据列表*/
	function index(){
		if (!$this->request->isAjax()){
			return view('index');
		}else{
			$limit  = $this->request->post('limit', 20, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;
            $where['wxapp_id'] = session('backend.wxapp_id');
			$order  = $this->request->post('order', '', 'serach_in');	//排序字段 bootstrap-table 传入
			$sort  = $this->request->post('sort', '', 'serach_in');		//排序方式 desc 或 asc
			$field = '*';
			$orderby = ($sort && $order) ? $sort.' '.$order : 'id desc';
			$res = NoticeService::indexList(formatWhere($where),$field,$orderby,$limit,$page);
			return json($res);
		}
	}



	/*修改*/
	function update(){
		if (!$this->request->isPost()){
			$id = $this->request->get('id','','serach_in');
			if(!$id) $this->error('参数错误');
			$this->view->assign('info',checkData(NoticeModel::find($id)));
			return view('update');
		}else{
			$postField = 'id,status';
			$data = $this->request->only(explode(',',$postField),'post',null);
			$res = NoticeService::update($data);
			return json(['status'=>'00','msg'=>'修改成功']);
		}
	}


	function updateExt(){
        $postField = 'id,status';
        $data      = $this->request->only(explode(',', $postField), 'post', null);
        if (!$data['id']) $this->error('参数错误');
        try {
            NoticeModel::update($data);
        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        return json(['status' => '00', 'msg' => '操作成功']);
    }
	/*查看详情*/
	function view(){
		$id = $this->request->get('id','','serach_in');
		if(!$id ) $this->error('参数错误');
        $info=NoticeModel::find($id);
        if($info['status']==0){
            $data=["id"=>$id,"status"=>1];
              $res=NoticeModel::update($data);
        }
		$this->view->assign('info',\app\api\model\Notice::find($id));

		return view('view');
	}



}

